<!--
  评论列表组件
  展示评论列表，包含用户头像、用户名、评论内容和时间等信息
-->
<template>
  <section class="comment-section">
    <header class="comment-header">
      <h3 class="comment-title">
        {{ title }}
        <span v-if="count !== undefined" class="comment-count">({{ count }})</span>
      </h3>
      <slot name="actions" />
    </header>
    <p v-if="!items.length" class="empty-text">{{ emptyMessage }}</p>
    <ul v-else class="comment-list">
      <li v-for="item in items" :key="item.id" class="comment-item">
        <div class="avatar" :style="item.avatarColor && { background: item.avatarColor }">
          <span v-if="item.avatar" class="avatar-image">
            <img :src="item.avatar" alt="" />
          </span>
          <span v-else class="avatar-text">{{ item.initial }}</span>
        </div>
        <div class="comment-body">
          <header class="comment-meta">
            <div class="meta-left">
              <span class="author">{{ item.author }}</span>
              <span v-if="item.badge" class="badge">{{ item.badge }}</span>
            </div>
            <time class="date">{{ item.createdAt }}</time>
          </header>
          <p class="content">{{ item.content }}</p>
        </div>
      </li>
    </ul>
  </section>
</template>

<script setup>
const props = defineProps({
  title: {
    type: String,
    default: '评论',
  },
  items: {
    type: Array,
    default: () => [],
  },
  emptyMessage: {
    type: String,
    default: '暂时还没有评论',
  },
  count: {
    type: Number,
    default: undefined,
  },
})

defineExpose({
  items: props.items,
})
</script>

<style scoped>
.comment-section {
  display: flex;
  flex-direction: column;
  gap: 16px;
  border: 1px solid #ebeef5;
  border-radius: 12px;
  padding: 20px;
  background: #fff;
}

.comment-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.comment-title {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 6px;
}

.comment-count {
  font-size: 14px;
  color: #909399;
  font-weight: normal;
}

.empty-text {
  margin: 0;
  color: #909399;
  font-size: 14px;
}

.comment-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.comment-item {
  display: flex;
  gap: 12px;
}

.avatar {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: #ecf5ff;
  color: #409eff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  flex-shrink: 0;
  overflow: hidden;
}

.avatar-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.comment-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.comment-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  font-size: 14px;
  color: #909399;
}

.meta-left {
  display: flex;
  align-items: center;
  gap: 8px;
}

.author {
  font-weight: 600;
  color: #303133;
}

.badge {
  padding: 2px 6px;
  border-radius: 999px;
  font-size: 12px;
  background: rgba(64, 158, 255, 0.12);
  color: #409eff;
}

.date {
  font-size: 13px;
}

.content {
  margin: 0;
  color: #303133;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
}
</style>

